 
 /**
 * controller.js
 * 简洁功能控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-16
 */

//留言添加控制器
app.controller('LeaveMsgAdd', function($scope, $http, $location, common, wechat){
	$scope.title 	= S('leavemsg_title') ? S('leavemsg_title'): '幸福快乐每一天!';
	$scope.param 	= S('leavemsg_param');
	$scope.content 	= '';
	
	if(!$scope.param){
		common.doAlert('留言参数错误!');
		common.backAnchor();
		return false;
	}

	common.setTitle('用户留言');
    wechat.wxConfig();

	//添加留言
	$scope.add_item 	= function(){
		var param 	= $scope.param;
			param['content']	= $scope.content;
		var url 	= config.prefix_url + '/LeaveMsg/addItem';
		
		$http.post(url, param).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			common.doAlert('谢谢留言！\n留言将由公众号筛选后显示，对所有人可见。');
			common.backAnchor();
		})
	}
})

//购买VIP
app.controller('VipPurchase', function($scope, $http, $location, common, wechat){
	$scope.userInfo 	= [];
	$scope.vipList 	= [];
	$scope.sVip 	= [];
	$scope.vipIntro 	= '';
	$scope.purchaseTip	= '';
	$scope.coupon 	= {code: '', status: 0, amount: 0, tip : ''};

	common.setTitle('“沫言育儿电台”购买');
    wechat.wxConfig();

	//加载数据
	$scope.load_data 	= function(){
		var url 	= config.prefix_url + '/User/getVipInfo?code='+config.code;

		$http.get(url).success(function(info){
			console.log(info);
			if(info.code != 1){
				common.dealError(info);
				return false;
			}
			$scope.userInfo 	= info.data.user_info;
			$scope.vipIntro 	= common.trustAsHtml(false, info.data.vip_intro);
			$scope.vipList 	= info.data.vip_info;
			if($scope.userInfo.vip_level){
				$scope.purchaseTip	= $scope.userInfo.vip_enddate + '到期，购买后有效期顺延。';
			}else{
				$scope.purchaseTip 	= $scope.userInfo.status ? 
					'会员已到期，请及时充值！' : 
					'“沫言”育儿电台，用心陪伴每一天。';
			}
			$scope.select_vip($scope.vipList[1]);
		})
	}

	//选择VIP
	$scope.select_vip 	= function(vip){
		for(var k in $scope.vipList){
			$scope.vipList[k].is_select 	= false;
		}
		vip.is_select 	= true;
		
		//选中VIP
		$scope.sVip 	= vip;
	}

	//监听优惠码变动
	$scope.$watch('coupon.code', function(nVal, oVal){
		if($scope.coupon.status == 1){
			$scope.coupon.status = 0;
			$scope.coupon.tip 	= '无效优惠码!';
			$scope.coupon.amount 	= 0;
		}
	});

	//检测优惠吗
	$scope.check_code 	= function(){
		$scope.coupon.tip 	= '检测中...';
		if(!$scope.coupon.code){
			$scope.coupon.tip 	= '无效优惠码!';
			$scope.coupon.status 	= 0;
			return false;
		}
		var url 	= config.prefix_url+'/Coupon/checkCode?code='+config.code;
		var param 	= {
			'code'	: $scope.coupon.code,
			'amount' : $scope.sVip.amount
		}
		$http.post(url, param).success(function(info){
			if(info.code != 1){
				$scope.coupon.status 	= 0;
				$scope.coupon.tip 	= info.data;
				$scope.coupon.amount 	= 0;
			}else{
				$scope.coupon.status 	= 1;
				$scope.coupon.amount 	= info.data.info.amount;
				$scope.coupon.tip 	= '该优惠券可抵消现金'+$scope.coupon.amount+'!';
				$('.am-modal.input-coupon').modal('close');
			}
			// $('.check-code-loading').css('display', 'none');
		});
	}

	//发起支付
	$scope.start_pay 	= function(){
		var url 	= config.prefix_url + '/User/purchaseVip?code='+config.code;
		var param 	= {
			'vip_id'	: $scope.sVip.id
		}
		if($scope.coupon.status == 1){
			param.coupon_code 	= $scope.coupon.code;
		}

		$http.post(url, param).success(function(info){
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			//发起支付
			wechat.wxPay(
				info.data.appId, 
				info.data.timeStamp, 
				info.data.nonceStr, 
				info.data.package, 
				info.data.signType, 
				info.data.paySign,
				function(){
					common.doAlert('购买成功!');
					WeixinJSBridge.call('closeWindow');
				}
			);
		})
	}

	$scope.load_data();
})

//幸福语录
app.controller('MottoIndex', function($scope, $http, $location, $timeout, common, wechat){
	//变量声明
	$scope.list 	= [];
	$scope.nRadio 	= new Object();
	$scope.imgUrl 	= '';

	common.setTitle('每日育儿经');
	wechat.wxConfig();

	//加载数据
	$scope.index 	= function(){
		var url 	= config.prefix_url + '/Motto/index?code='+config.code;
		$http.get(url).success(function(info){
			console.log(info);
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.list 	= info.data.list;
			$scope.userInfo 	= info.data.user_info;
			$scope.nRadio 	= $scope.list.shift();
			//初始化音频
			$scope.init_audio($scope.list);

			$timeout(function(){
				$scope.nRadio.aplayer 	= common.initAudio(document.getElementById('aplayer-'+$scope.nRadio.id), {
					'title'	: $scope.nRadio.title, 
					'author' 	: $scope.nRadio.online_date,
					'audio'	: $scope.nRadio.audio
				});
				$scope.nRadio.aplayer.on('play', function(){
					$scope.$apply(function(){
						$scope.nRadio.playing 	= true;
					})
				})

				$scope.nRadio.aplayer.on('pause', function(){
					$scope.$apply(function(){
						$scope.nRadio.playing 	= false;
					})
				})
			}, 300)
				

			//图片生成
			var url 	= config.prefix_url + '/Share/mottoImage?';
			var param 	= 'param='+base64encode('user_id='+$scope.userInfo.id+'&motto_id='
				+$scope.nRadio.id);

			$scope.imgUrl 	= url + param;
			console.log($scope.imgUrl);
		});
	}

	//初始化音频
	$scope.init_audio 	= function(list){
		var init_aplayer 	= function(item){
			item.online_date 	= item.online_date ? item.online_date : '欣乐土';
			item.aplayer 	= common.initAudio(document.getElementById('aplayer-'+item.id), {
				'title'	: item.title, 
				'author' 	: item.online_date,
				'audio'	: item.audio
			});

			// item.aplayer.on('play', function(){
			// 	$scope.$apply(function(){
			// 		item.playing 	= true;
			// 	})
			// })

			// item.aplayer.on('pause', function(){
			// 	$scope.$apply(function(){
			// 		item.playing 	= false;
			// 	})
			// })
		}
		
		$timeout(function(){
			for(var k in list){
				var item 	= list[k];

				//初始化音频，解决异步问题
				init_aplayer(item);
			}
		}, 300);	
	}

	//控制音频
	$scope.toggle_audio 	= function(aplayer){
		console.log('run this');
		aplayer.toggle();
	}

	$scope.index();
})

//社区入口
app.controller('BBSIndex', function($scope, $http, $location, $timeout, common, wechat){

	common.setTitle('健康社区');
	$scope.bbsImg 	= '';

	$scope.index 	= function(){
		//检测是否可进入bbs
		var url 	= config.prefix_url + '/RadioMorning/showBBS?code='+config.code;
		$http.get(url).success(function(res){
			if(res.code != 1){
				common.dealError(res);
				return false;
			}

			$scope.bbsImg 	= '../image/bbs_join.jpg';
		})
	}

	$scope.index();
})
